//
//  LZMyCouponsTableViewCell.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/31.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyCouponsTableViewCell.h"

#import "Masonry.h"
#import "LZMyCouponsModel.h"

@implementation LZMyCouponsTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self configView];
        
        [self addAutoLayout];
        
    }
    return self;
}

- (void)configView {

    [self.contentView addSubview:self.bgImageView1];
    
    [self.bgImageView1 addSubview:self.tuanImageView];
    [self.bgImageView1 addSubview:self.tuanSubLabel];
    [self.bgImageView1 addSubview:self.juanPriceLabel];
    [self.bgImageView1 addSubview:self.subLable];
    
    [self.contentView addSubview:self.bgImageView2];
    [self.bgImageView2 addSubview:self.contentLabel];
    [self.bgImageView2 addSubview:self.selectedCellMark];
    [self.bgImageView2 addSubview:self.stateImage];
}

- (void)addAutoLayout {
    
    WS(weakSelf);

    [self.bgImageView1 makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.leading.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(95.5, 76));
    }];
    
    [self.tuanImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(18);
        make.centerX.mas_equalTo(weakSelf.bgImageView1.mas_centerX);
    }];
    
    [self.tuanSubLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tuanImageView.mas_bottom).offset(6);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.equalTo(10);
    }];
    
    [self.juanPriceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(19);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.equalTo(31);
    }];
    
    [self.subLable makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.juanPriceLabel.mas_bottom).with.offset(6);
        make.leading.mas_equalTo(20);
        make.trailing.mas_equalTo(-20);
        make.height.equalTo(8);
    }];
    
    [self.bgImageView2 makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.leading.mas_equalTo(weakSelf.bgImageView1.mas_trailing);
        make.trailing.mas_equalTo(-20);
        make.height.mas_equalTo(76);
    }];
    
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.bottom.mas_equalTo(-20);
        make.leading.mas_equalTo(20);
        make.trailing.mas_equalTo(-25);
    }];
    
    [self.selectedCellMark mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.equalTo(16);
        make.bottom.equalTo(-5);
        make.trailing.equalTo(-10);
    }];
    
    [self.stateImage makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-3);
        make.trailing.mas_equalTo(0);
    }];
}

- (void)setModel:(LZMyCouponsModel *)model {
    _model = model;
    
    if (self.viewType == 2) {
        self.stateImage.hidden = NO;
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            self.stateImage.image = [UIImage imageNamed:@"my_coupon_used_ar"];
        } else {
            self.stateImage.image = [UIImage imageNamed:@"my_coupon_used_en"];
        }
    }
    else if(self.viewType == 3) {
        if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
            self.stateImage.image = [UIImage imageNamed:@"my_coupon_expired_ar"];
        } else {
            self.stateImage.image = [UIImage imageNamed:@"my_coupon_expired_en"];
        }
    }
    else {
        self.stateImage.hidden = YES;
    }

    if ([model.promotionType integerValue] == 1) {
        // 优惠券
        
        self.tuanImageView.hidden = YES;
        self.tuanSubLabel.hidden = YES;
        self.juanPriceLabel.hidden = NO;
        self.subLable.hidden = NO;
        
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            [self.bgImageView1 setImage:[[UIImage imageNamed:@"lz_mycoupons_juan_cellbgimage_1_ar"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)
                                         resizingMode:UIImageResizingModeStretch]];
        } else {
            [self.bgImageView1 setImage:[[UIImage imageNamed:@"lz_mycoupons_juan_cellbgimage_1_en"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)
                                         resizingMode:UIImageResizingModeStretch]];
        }
        
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            [self.bgImageView2 setImage:[[UIImage imageNamed:@"lz_mycoupons_juan_cellbgimage_2_ar"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)
                                         resizingMode:UIImageResizingModeStretch]];
        } else {
            [self.bgImageView2 setImage:[[UIImage imageNamed:@"lz_mycoupons_juan_cellbgimage_2_en"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 70)
                                         resizingMode:UIImageResizingModeStretch]];
        }
        
        self.juanPriceLabel.text = [NSString stringWithFormat:@"%@", model.couponsMoney];
        self.contentLabel.text = [NSString stringWithFormat:@"%@", model.promotionName];
    }
    else if ([model.promotionType integerValue] == 2) {
        // 团购
        
        self.tuanImageView.hidden = NO;
        self.tuanSubLabel.hidden = NO;
        self.juanPriceLabel.hidden = YES;
        self.subLable.hidden = YES;
        
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            [self.bgImageView1 setImage:[[UIImage imageNamed:@"lz_mycoupons_tuan_cellbgimage_1_ar"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)
                                         resizingMode:UIImageResizingModeStretch]];
        } else {
            [self.bgImageView1 setImage:[[UIImage imageNamed:@"lz_mycoupons_tuan_cellbgimage_1_en"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)
                                         resizingMode:UIImageResizingModeStretch]];
        }
        
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            [self.bgImageView2 setImage:[[UIImage imageNamed:@"lz_mycoupons_tuan_cellbgimage_2_ar"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)
                                         resizingMode:UIImageResizingModeStretch]];
        } else {
            [self.bgImageView2 setImage:[[UIImage imageNamed:@"lz_mycoupons_tuan_cellbgimage_2_en"]
                                         resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)
                                         resizingMode:UIImageResizingModeStretch]];
        }
        
        self.contentLabel.text = [NSString stringWithFormat:@"%@", model.promotionName];
    }
}

#pragma mark - Setter/Getter

- (UIImageView *)bgImageView1 {

    if (_bgImageView1 == nil) {
        _bgImageView1 = [[UIImageView alloc] init];
    }

    return _bgImageView1;
}

- (UIImageView *)tuanImageView {

    if (_tuanImageView == nil) {
        _tuanImageView = [[UIImageView alloc] init];
        _tuanImageView.image = [UIImage imageNamed:@"lz_mycoupon_tuan_icon"];
    }
    
    return _tuanImageView;
}

- (UILabel *)tuanSubLabel {
    if (_tuanSubLabel == nil) {
        _tuanSubLabel = [[UILabel alloc] init];
        _tuanSubLabel.font          = [UIFont systemFontOfSize:10];
        _tuanSubLabel.textAlignment = NSTextAlignmentCenter;
        _tuanSubLabel.textColor = UIColorFromRGB(0xffffff, 1);
        _tuanSubLabel.text = LZLocalizedString(@"group_order", nil);
    }
    return _tuanSubLabel;
}

- (UILabel *)juanPriceLabel {

    if (_juanPriceLabel == nil) {
        _juanPriceLabel = [[UILabel alloc] init];
        _juanPriceLabel.font            = [UIFont systemFontOfSize:41];
        _juanPriceLabel.textColor       = UIColorFromRGB(0xffffff, 1);
        _juanPriceLabel.textAlignment   = NSTextAlignmentCenter;
    }
    
    return _juanPriceLabel;
}

- (UILabel *)subLable {

    if(_subLable == nil) {
        _subLable = [[UILabel alloc] init];
        _subLable.font          = [UIFont systemFontOfSize:10];
        _subLable.textAlignment = NSTextAlignmentCenter;
        _subLable.textColor = UIColorFromRGB(0xfdfdfd, 1);
        _subLable.text = @"BHD";
    }
   
    return _subLable;
}

- (UIImageView *)bgImageView2 {

    if (_bgImageView2 == nil) {
        _bgImageView2 = [[UIImageView alloc] init];
    }

    return _bgImageView2;
}

- (UILabel *)contentLabel {

    if (_contentLabel == nil) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font          = [UIFont systemFontOfSize:15];
        _contentLabel.textColor     = UIColorFromRGB(0xffffff, 1);
    }
    
    return _contentLabel;
}

- (UIImageView*)selectedCellMark {
    if (_selectedCellMark) {
        return _selectedCellMark;
    }
    
    _selectedCellMark = [[UIImageView alloc] init];
    _selectedCellMark.image = [UIImage imageNamed:@"选中_优惠买单"];
    _selectedCellMark.hidden = YES;
    return _selectedCellMark;
}

- (UIImageView *)stateImage {

    if (_stateImage == nil) {
        _stateImage = [[UIImageView alloc] init];
        _stateImage.hidden = YES;
    }
    
    return _stateImage;
}

@end
